#include "text.h"

namespace poly
{
Text::Text(){}
Text::~Text()
{
	if(fontInterfaceCreated) fontInterface->Release();
	if(textString != NULL)	delete [] textString;
	device_ptr = 0;
}
void Text::setup(D3DDevice device)
{
	device_ptr = device;

	fontD.Height		  = 80;
	fontD.Width           = 40;
    fontD.Weight          = FW_NORMAL;
    fontD.MipLevels       = 0;
    fontD.Italic          = false;
    fontD.CharSet         = DEFAULT_CHARSET;
    fontD.OutputPrecision = OUT_DEFAULT_PRECIS;
    fontD.Quality         = DEFAULT_QUALITY;
    fontD.PitchAndFamily  = DEFAULT_PITCH | FF_DONTCARE;

    setFace("Arial");
	
	textQuad.top		  = 5;
	textQuad.bottom		  = 5;
	textQuad.left		  = 0;
	textQuad.right		  = 0;
	color				  = float3(256,256,256);

	textString			  = NULL;

	changed				  = true;
	fontInterfaceCreated  = false;
}
void Text::setWidth(int value)
{
	fontD.Width = value;
	changed = true;
}
void Text::setHeight(int value)
{
	fontD.Height = value;
	fontD.Width	 = value/2.2;
	changed = true;
}
void Text::setWeight(int value)
{
	fontD.Weight = value;
	changed = true;
}
void Text::setItalic(bool value)
{
	fontD.Italic = value;
	changed = true;
}
void Text::setFace(char* value)
{
	//type Value to Type text
	int len = strlen(value) + 1;
	text* face = new text [len];
	mbstowcs(face, value, len);	
	_tcscpy(fontD.FaceName, face);
	
	changed = true;
}
void Text::setPosition(float2 value)
{
	textQuad.top  = value.x;
	textQuad.left = value.y;
}
void Text::setText(char* value)
{
	//type Value to Type text
	int len = strlen(value) + 1;
	delete [] textString;
	textString = new text [len];
	mbstowcs(textString, value, len);
}
void Text::setColor(float3 value)
{
	color = value;
}
void Text::draw()
{
	if(changed) 
	{
		D3DXCreateFontIndirect(device_ptr, &fontD, &fontInterface);
		fontInterfaceCreated = true;
	}
	changed = false;

	fontInterface->DrawText(0, (LPCWSTR)textString, -1, &textQuad, DT_NOCLIP, D3DCOLOR_XRGB((int)color.x, (int)color.y, (int)color.z));
}
void Text::onLost()
{
	fontInterface->OnLostDevice();
}
void Text::onReset()
{
	fontInterface->OnResetDevice();
}

}//end of poly